#include<owl\applicat.h>
#include<owl\framewin.h>
#include<owl\dc.h>
#include<owl\gdiobjec.h>
#include<string.h>
#include<owl\dialog.h>
#include<owl\radiobut.h>
#include<owl\groupbox.h>
#include<owl\checkbox.h>
#include<owl\menu.h>

#pragma hrdstop

#include"corners.rh"
#define false 0
#define true 1
#define TIMER_ID 10
#define TIMER    11

//*********************************************************
#include "corner.h"
#include "mainwin.h"
void extern MySound(int n,int s);
//*********************************************************
class TOptionDialog:public TDialog
{
  TCheckBox* sound;
  TCheckBox* black;
  TCheckBox* way;
  TGroupBox* group;
  Options* options;
  TRadioButton * rb[9];
  void SetOptionSound()
   {
    if(sound->GetCheck()==BF_CHECKED) options->sound=1;
      else options->sound=0;
   }
  void SetOptionBlack()
   {
    if(black->GetCheck()==BF_CHECKED) options->blackBegin=1;
       else options->blackBegin=0;
   }
  void SetOptionWay()
   {
    if(way->GetCheck()==BF_CHECKED) options->way=1;
       else options->way=0;
   }
  void SetOptionDeepness()
   {
    for(int i=0;i<9;i++)
     {
      if(rb[i]->GetCheck() == BF_CHECKED)
        options->deepness=i;
     }
   };
 public:
  TOptionDialog(TWindow*parent,Options&op):
     TDialog(parent,ID_OPTION_DIALOG_16)
      {
       options=&op;
       sound=new TCheckBox(this,ID_OPTION_SOUND);
       black=new TCheckBox(this,ID_OPTION_BLACK);
       way=new TCheckBox(this,ID_OPTION_WAY);
       group=new TGroupBox(this,IDC_GROUPBOX);
       for(int i=0;i<9;i++)
        rb[i]=new TRadioButton(this,IDC_RB0+i,group);
      }
 void
   SetupWindow()
    {
    TDialog::SetupWindow();
    if(options->sound==1) sound->Check();
    if(options->blackBegin==1) black->Check();
    if(options->way==1) way->Check();
    rb[options->deepness]->Check();
    }

  DECLARE_RESPONSE_TABLE(TOptionDialog);
};
  DEFINE_RESPONSE_TABLE1(TOptionDialog,TDialog)
    EV_COMMAND(ID_OPTION_SOUND,SetOptionSound),
    EV_COMMAND(ID_OPTION_BLACK,SetOptionBlack),
    EV_COMMAND(ID_OPTION_WAY,SetOptionWay),
    EV_COMMAND(IDC_RB0,SetOptionDeepness),
    EV_COMMAND(IDC_RB1,SetOptionDeepness),
    EV_COMMAND(IDC_RB2,SetOptionDeepness),
    EV_COMMAND(IDC_RB3,SetOptionDeepness),
    EV_COMMAND(IDC_RB4,SetOptionDeepness),
    EV_COMMAND(IDC_RB5,SetOptionDeepness),
    EV_COMMAND(IDC_RB6,SetOptionDeepness),
    EV_COMMAND(IDC_RB7,SetOptionDeepness),
    EV_COMMAND(IDC_RB8,SetOptionDeepness),
  END_RESPONSE_TABLE;

//*********************************************************
 TCornersWin::TCornersWin(TWindow*parent,const char far*title):
	  TFrameWindow(parent,title),
	  TWindow(parent,title)
	{
	 AssignMenu(ID_MENU);
	 blackBegin=0;
    keyboard=0;
    sound=0;
    options.blackBegin=false;
    options.sound=false;
    options.way=true;
    deepness=1;
    options.deepness=deepness;
	 first=1;
	 flagSnd=0;
	 corners=0;
    TBitmap b(*GetApplication(),BITMAP_DESK);
	 Attr.X=(GetSystemMetrics(SM_CXSCREEN)-b.Width()-7)/2;
	 Attr.Y=(GetSystemMetrics(SM_CYSCREEN)-b.Height()-44)/2;
	 Attr.W=b.Width()+7;
	 Attr.H=b.Height()+44;
	 Attr.Style &= ~(WS_MAXIMIZEBOX|WS_THICKFRAME);
	 flag=0;
	}
  TCornersWin::~TCornersWin()
	{
	 KillTimer(TIMER);
	 KillTimer(TIMER_ID);
	 delete corners;
    delete popupMenu;
	}
  void TCornersWin::SetupWindow()
	{
	  TFrameWindow::SetupWindow();
	  HINSTANCE i=GetModule()->GetInstance();
	  corners=new TCorners(this,GetModule()->GetInstance());
     corners->SetDeepness(deepness);
	  SetTimer(TIMER,10);
	  SetTimer(TIMER_ID,200);
     popupMenu=new TPopupMenu();
     popupMenu->AppendMenu(MF_STRING,CM_UNDO,"Отменить последний ход");
	}
  void TCornersWin::Play(int n,int s)
	{
     if(sound)
       MySound(n,s);
	}
  void TCornersWin::Paint(TDC&dc,bool d,TRect&rect)
	{
	  corners->Draw();
	}
  void TCornersWin::CmRestart()
	{
	 first=1;
    flag=0;
	 corners->Restart();
    if(blackBegin)
     {
      corners->CompGo();
      first=0;
     }
    Invalidate();
   }
  void TCornersWin::CmUndo()
		  {
			try{
				corners->Undo();
				flag=0;
				Invalidate();
				}
			catch(char*s)
			 {
			  MessageBox(s,"ВНИМАНИЕ!!!",MB_OK|MB_ICONWARNING);
			  flag=0;
			 }
		  }
  void TCornersWin::CmBlackBegin()
     {
      options.blackBegin=blackBegin;
      options.sound=sound;
      options.way=corners->WayIs();
      options.deepness=deepness;
      TOptionDialog* d=new TOptionDialog(this,options);
      if(d->Execute()==IDOK)
        {
         blackBegin=options.blackBegin;
         deepness=options.deepness;
         corners->SetDeepness(deepness);
         if(options.way)
               corners->WayOn();
            else
               corners->WayOff();
         sound=options.sound;
         if(first&&blackBegin)
           {
            corners->CompGo();
            first=0;
           }
        }
       delete d;
     }
  void TCornersWin::CmDeepness()
	 {
		Invalidate();
	 }
  void TCornersWin::PlayRolik(int x,int y){}
  void TCornersWin::EvTimer(UINT timerId)
	 {
		switch(timerId){
		case TIMER:
		  if(corners->FlagPlay()&&index<20)
			 {
			  corners->Play(index);
			  index++;
			  flag=0;
			  if(index>=19) Play(1,1);
			  return;
			 }
			 else
				{
				 index=0;
				}
          break;
		 case TIMER_ID:
			 if(flag)
				 {
				  corners->Jump();
				  if(!flagSnd){Play(1,4);flagSnd=1;}
					 else flagSnd=0;
				 }
			 break;
							}
	 }
 DEFINE_RESPONSE_TABLE1(TCornersWin,TFrameWindow)
  EV_WM_LBUTTONDOWN,
  EV_WM_RBUTTONDOWN,
  EV_COMMAND(CM_RESTART,CmRestart),
  EV_COMMAND(CM_UNDO,CmUndo),
  EV_COMMAND(CM_BLACK,CmBlackBegin),
  EV_COMMAND(CM_DEEPNESS,CmDeepness),
  EV_WM_TIMER,
 END_RESPONSE_TABLE;
void
 TCornersWin::EvRButtonDown(UINT modKeys,TPoint& point)
  {
   TPoint mouse(point);
   ClientToScreen(mouse);
   popupMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,mouse,0,HWindow);
  }
void
 TCornersWin::EvLButtonDown(UINT modKeys,TPoint& point)
	{
	  if(!flag)
		 {
		  try
			{
			 corners->From(point.x,point.y);
			 if(corners->FlagPlay())
				 {
				  flag=0;
				  return;
				 }
			  else flag=1;
			 flagSnd=0;
			}
		  catch(char*str)
			{
			 index=0;
			 MessageBox(str,"ВНИМАНИЕ!!!",MB_OK);
			 flag=0;
			}
		 }
	  else
		 {
		  try
			{
			 if(corners->FlagPlay()) {flag=0;return;}
			 corners->To(point.x,point.y);
          first=0;
			 int end=0;
			 if(corners->IsEnd()==1) end=corners->IsEnd();
			 flag=0;
			 int t=0;
			 if(!end)
				try
            {
             corners->CompGo();
            }
            catch(char* str)
             {
              if(MessageBox("Я сдаюсь\n\tСначала?","ВЫ ПОБЕДИЛИ",MB_YESNO)==IDYES)
             		 {corners->Restart();Invalidate();}
                else CloseWindow();
             }
				else
				 if(MessageBox("Выиграл светлый\n\tСначала?","ПОБЕДА!!!",MB_YESNO)==IDYES)
						 {corners->Restart();Invalidate();}
					 else CloseWindow();
			 if(corners->IsEnd()==2)
				 if(MessageBox("Выиграл темный\n\tСначала?","ПОБЕДА!!!",MB_YESNO)==IDYES)
						 {corners->Restart();Invalidate();}
					 else CloseWindow();
			}
		  catch(char*str)
			{
			 MessageBox(str,"ВНИМАНИЕ!!!",IDOK);
			 flag=0;
			}
		  catch(int)
			{
			 corners->From(point.x,point.y);
			 flag=1;
			 flagSnd=0;
			}
		  catch(double)
			{
			  if(MessageBox("Я сдаюсь у меня нет хода\n\tСначала?","ПОБЕДА!!!",MB_OKCANCEL)==IDOK)
			  {
				flag=0;
				corners->Restart();
				Invalidate();
			  }
				  else {CloseWindow();}
			}
		 }

	}

TCornersApp::TCornersApp(const char far*name):
	 TApplication(name){}

//*********************************************************

void TCornersApp::InitMainWindow()
	{
    //EnableBWCC();
    //EnableCtl3d();
	 MainWindow=new TCornersWin(0,"Уголки (авторы: Мисюра Виталий & Тимченко Юля)");
    MainWindow->SetIcon(this,ID_ICON);
	}

int OwlMain(int argc,char* argv[])
 {

  TCornersApp app("Corners");
  return app.Run();
 }


